package 临时;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtils;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.DefaultDrawingSupplier;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.RingPlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.junit.Test;

import java.awt.*;
import java.io.*;

public class test4 {
    @Test
    public void annular(){
        DefaultPieDataset dataSet = new DefaultPieDataset();
        int i, j;
        String[] key = { "私募股权","房地产","基金","现金宝宝","股票" };
        int[] value = { 150, 20, 325, 78, 105 };
        for (i = 0, j = 0; i < key.length && j < value.length; i++, j++) {
            dataSet.setValue(key[i], value[j]);
        }
        JFreeChart chart = ChartFactory.createRingChart("2021年收入", dataSet, true, false, false);
        // 设置显示标注
        chart.getLegend().setVisible(true);
        Font font = new Font("宋体", Font.BOLD, 15);
        TextTitle textTitle = chart.getTitle();
        textTitle.setFont(font);// 为标题设置上字体

        Font plotFont = new Font("宋体", Font.PLAIN, 16);
        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setLabelFont(plotFont); // 为饼图元素设置上字体

        Font LegendFont = new Font("楷体", Font.PLAIN, 18);
        LegendTitle legend = chart.getLegend(0);
        legend.setItemFont(LegendFont);// 为图例说明设置字体
        chart.getLegend().setItemFont(font);
        // 标注位于上侧
        // chart.getLegend().setPosition(RectangleEdge.TOP);
        // 设置标注无边框
        chart.getLegend().setFrame(new BlockBorder(Color.WHITE));

        // 环形图
        RingPlot ringplot = (RingPlot) chart.getPlot();
        ringplot.setOutlineVisible(false);
        //{2}表示显示百分比  //{0}:key {1}:value {2}:百分比 {3}:sum
        ringplot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}:{1}"));
        ringplot.setBackgroundPaint(new Color(253,253,253));
        ringplot.setOutlineVisible(false);
        /*//设置标签样式
        ringplot.setLabelFont(new Font("宋体", Font.BOLD, 15));
        ringplot.setSimpleLabels(true);
        ringplot.setLabelLinkPaint(Color.WHITE);
        ringplot.setLabelOutlinePaint(Color.WHITE);
        ringplot.setLabelLinksVisible(false);
        ringplot.setLabelShadowPaint(null);
        ringplot.setLabelOutlinePaint(new Color(0,true));
        ringplot.setLabelBackgroundPaint(new Color(0,true));
        ringplot.setLabelPaint(Color.WHITE);*/

//        ringplot.setSectionOutlinePaint(Color.WHITE);
        ringplot.setSeparatorsVisible(true);
        ringplot.setSeparatorPaint(Color.WHITE);
        ringplot.setShadowPaint(new Color(253,253,253));
        // 设置深度，即带颜⾊圆环的宽度
        ringplot.setSectionDepth(0.4);
        ringplot.setStartAngle(90);
        // 指定颜色
        ringplot.setDrawingSupplier(new DefaultDrawingSupplier(
                new Paint[] {
                        new Color(0, 0 , 222),
                        new Color(174, 145, 195),
                        new Color(255, 162, 195),
                        new Color(249, 163, 86),
                        new Color(119, 173, 195)
                },
                DefaultDrawingSupplier.DEFAULT_OUTLINE_PAINT_SEQUENCE,
                DefaultDrawingSupplier.DEFAULT_STROKE_SEQUENCE,
                DefaultDrawingSupplier.DEFAULT_OUTLINE_STROKE_SEQUENCE,
                DefaultDrawingSupplier.DEFAULT_SHAPE_SEQUENCE));

        //在D盘目录下生成图片
        int width = 800;
        int height = 600;
        File p = new File("d://");
        if (!p.exists()) {
            p.mkdirs();
        }
        String imageName = System.currentTimeMillis() + "_环形图" + ".jpeg";
        File file = new File(p.getPath() + "/" + imageName);
        try {
            if(file.exists()) {
                file.delete();
            }
            ChartUtils.saveChartAsJPEG(file, chart, width, height);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    private Font getFont(int style, Float size) {
        Font defFont = new Font("黑体", style, 19);

        try {
            File file = initFontFile();
            if (!file.exists()) {
                return defFont;
            }
            Font nf = Font.createFont(Font.TRUETYPE_FONT, file);
            nf = nf.deriveFont(style, size);
            return nf;
        } catch (Exception e) {
        }
        return defFont;
    }
    private File initFontFile() throws IOException {
        String fileName = "simhei.ttf";
        String filepath = getUploadResource(fileName);

        return new File(filepath);
    }
    public String getUploadResource(String fileName) throws IOException {
        //若文件已存在，则返回的filePath中含有"EXIST"，则不需再重写文件
        String filePath = createFile(fileName);

        //文件不存在，则创建流输入默认数据到新文件
        if (!filePath.contains("exist")) {
            File file = new File(filePath);
            try (OutputStream os = new FileOutputStream(file); InputStream is = getClass().getClassLoader().getResourceAsStream(fileName)) {
                //返回读取指定资源的输入流
                int bytesRead = 0;
                byte[] buffer = new byte[1024];
                if (is != null) {
                    while ((bytesRead = is.read(buffer, 0, 1024)) != -1) {
                        os.write(buffer, 0, bytesRead);
                    }
                } else {
                }
            } catch (FileNotFoundException e) {
            } catch (IOException e) {
            }
            return filePath;
        }
        return filePath.substring(5);
    }


    public static String createFile(String filename) {
        String dirPath = System.getProperty("user.dir") + /*Constants.M_GUARD_PATROL + */"/uploadFiles";
        File dir = new File(dirPath);
        dir.mkdirs();

        //create file
        String filePath = dirPath + File.separator + filename;
        File file = new File(filePath);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
            }
            return filePath;
        }
        return "exist" + filePath;
    }
}
